﻿#include <QtCore>

namespace helper {

QByteArray ReadDataFromFile(const QString& path) {
  QFile tmpFile(path);
  tmpFile.open(QIODevice::ReadOnly);
  auto bytes = tmpFile.readAll();
  tmpFile.close();
  return bytes;
}

bool SaveContent2File(const QByteArray& data, const QString& path, bool enable_log) {
  //写入到新的文件
  QFile file(path);
  file.remove();
  if (!file.open(QIODevice::WriteOnly)) {
    qWarning() << "save file fail, path:" << path << " failed, error:" << file.errorString();
    return false;
  } else {
    file.write(data);
    file.flush();
    if (enable_log) {
      qDebug() << "save file success, path:" << path;
    }
  }
  file.close();
  return true;
}

bool CopyFile2Target(const QString& source, const QString& target) {
  if (QFile::exists(target)) {
    return true;
  }
  if (!source.endsWith('/')) {
    QFile::remove(target);
    QFile::copy(source, target);
  } else {
    qWarning() << "invalid file path:" << source;
  }
  return false;
}

}  // namespace helper
